<?php

if (!defined('IN_XLP')) {
    exit('Access Denied!');
}

/**
 * Description of powerAction
 *
 * @author xlp
 */
class powerAction extends commonAction {

    function __construct() {
        parent::__construct();
        parent::_checkLogin();
        $this->status = array(0 => '<span class="red">申请</span>', 1 => '通过', 2 => '<span class="grey">无效</span>');
    }

    function index() {
        $p = $this->_getid('p', 1);
        $uid = $this->_getid('uid', 0);
        $village_id = $this->_getid('village_id', 0);
        $status = $this->_getid('status', 0);
        $where = array();
        if ($uid) {
            $where = array('uid' => $uid);
        }
        if ($village_id) {
            $where = array('village_id' => $village_id);
        }
        if ($status) {
            $where = array('status' => ($status - 1));
        }
        $list = C('village_admin');
        $type = array('<span class="grey">×</span>', '<span class="red">√</span>');
        $rs = D('villageAdmin')->where($where)->page($p)->order('id DESC')->findAll();
        $total = D('villageAdmin')->getTotal();
//        z(M('area')->getAllSql());
        if ($rs) {
            foreach ($rs as $k => $v) {
                $rs[$k]['user_name'] = getUser($v['uid']);
                $rs[$k]['village_name'] = parent::getVillageName($v['village_id']);
            }
        }
        $this->assign(array('rs' => $rs, 'total' => $total, 'p' => $p, 'status' => $this->status, 'list' => $list, 'type' => $type));
//        print_r($this->getAllVar());
        $this->display();
    }

    function add() {
        $uid = $this->_getid('uid');
        if (!$uid) {
            showError('参数丢失');
        }
        $rs = D('member')->getUserInfoById($uid);
        if (!$rs) {
            showError('用户不存在');
        }
        $rs['village_name'] = parent::getVillageName($rs['village_id']);
        $has = D('villageAdmin')->where(array('uid' => $rs['uid'], 'village_id' => $rs['village_id']))->find();
        if ($has) {
            jumpTo(U('power/edit', array('id' => $has['id'])));
        }
        $list = C('village_admin');
        $this->assign(array('rs' => $rs, 'status' => $this->status, 'list' => $list));
        $this->display();
    }

    function edit() {
        $id = $this->_getid('id');
        if (!$id) {
            showError('参数丢失');
        }
        $list = C('village_admin');

        $rs = D('villageAdmin')->where(array('id' => $id))->find();
        if (!$rs) {
            showError('记录不存在');
        }
        $rs['username'] = getUser($rs['uid']);
        $rs['village_name'] = parent::getVillageName($rs['village_id']);
        $this->assign(array('rs' => $rs, 'status' => $this->status, 'list' => $list));
        $this->display();
    }

    function post() {
        $objData = array();
        //获取字段内容
        $fieldList = D('villageAdmin')->getTableFields();
        foreach ($fieldList['fields'] as $key => $val) {
            $objData[$key] = $val['type'] == 'int' ? $this->_postid($key, $val['value']) : $this->_post($key, $val['value']);
        }
        $objData['infotime'] = TIME;
        if (!$objData['uid']) {
            $this->JsonReturn('用户不能为空');
        }
//        z($objData);
        //检查是否重复
        $rs = D('villageAdmin')->where(array('uid' => $objData['uid'], 'village_id' => $objData['village_id']))->find();
        if ($rs) {
            $id = $rs['id'];
            unset($objData['uid'], $objData['village_id'], $objData['infotime']);
            D('villageAdmin')->update($objData, array('id' => $id));
        } else {
            $id = D('villageAdmin')->insert($objData);
        }
        D('villageAdmin')->clearAllowCache();
        $this->JsonReturn('ok', $id, 1);
    }

    function save() {
        $id = $this->_postid('id');
        if (!$id) {
            showError('参数丢失');
        }
        $rs = D('villageAdmin')->where(array('id' => $id))->find();
        if (!$rs) {
            showError('记录不存在');
        }
        $objData = array();
        //获取字段内容
        $fieldList = D('villageAdmin')->getTableFields(array('uid', 'infotime', 'village_id'));
        foreach ($fieldList['fields'] as $key => $val) {
            $objData[$key] = $val['type'] == 'int' ? $this->_postid($key, $val['value']) : $this->_post($key, $val['value']);
        }
        D('villageAdmin')->update($objData, array('id' => $id));
        D('villageAdmin')->clearAllowCache();
        $this->JsonReturn('ok', null, 1);
    }

    function delete() {
        $id = $this->_postid('id');
        if (!$id) {
            showError('参数丢失');
        }
        $rs = D('villageAdmin')->where(array('id' => $id))->find();
        if (!$rs) {
            showError('记录不存在');
        }
        D('villageAdmin')->delete(array('id' => $id));
        D('villageAdmin')->clearAllowCache();
        $this->JsonReturn('ok', null, 1);
    }

}
